package cn.net.xyan.easy.webapp.base.frontend.ui;

import cn.net.xyan.easy.gwt.client.EZGwt;
import cn.net.xyan.easy.gwt.client.model.TableCellDataModel;
import cn.net.xyan.easy.gwt.client.ui.Table;
import cn.net.xyan.easy.gwt.client.ui.TableDelegate;
import cn.net.xyan.easy.gwt.client.ui.cell.ButtonItemCell;
import cn.net.xyan.easy.gwt.shared.annotation.DynamicGWTObject;
import cn.net.xyan.easy.webapp.base.api.dto.CommitRegisterUserReq;
import cn.net.xyan.easy.webapp.base.api.dto.QueryUserRsq;
import cn.net.xyan.easy.webapp.base.frontend.provider.UserProvider;
import cn.net.xyan.easy.webapp.base.frontend.service.SystemConfigControllerAsync;
import cn.net.xyan.easy.webapp.base.frontend.ui.editor.NewUserEditor;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.*;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.MethodCallback;
import org.gwtbootstrap3.client.ui.Modal;
import org.gwtbootstrap3.extras.bootbox.client.Bootbox;

import java.util.List;

/**
 * Created by zarra on 16/3/10.
 */
@DynamicGWTObject
public class UserView extends Composite {
    @UiTemplate("UserView.ui.xml")
    interface Binder extends UiBinder<Widget, UserView > {}

    interface Driver extends SimpleBeanEditorDriver<CommitRegisterUserReq,NewUserEditor>{}



    private static Binder binder = GWT.create(Binder.class);

    private Driver driver = GWT.create(Driver.class);

    private CommitRegisterUserReq registerUserReq;

    private SystemConfigControllerAsync scca = EZGwt.create(SystemConfigControllerAsync.class);

    @UiField
    Table table;

    @UiField
    NewUserEditor newUserEditor;

    @UiField
    Modal addModal;

    Delegate delegate;

    UserProvider provider;

    public static class Delegate implements TableDelegate<TableCellDataModel>{

        @Override
        public int numberOfColumn() {
            return 4;
        }

        @Override
        public Column<TableCellDataModel, ?> column(final int columnIndex) {
            return new TextColumn<TableCellDataModel>() {
                @Override
                public String getValue(TableCellDataModel object) {
                    return object.valueForColumnIndex(String.class, columnIndex);
                }
            };
        }

        @Override
        public String columnName(int columnIndex) {
            switch (columnIndex){
                case 0:
                    return "UserName";
                case 1:
                    return "RealName";
                case 2:
                    return "Email";
                case 3:
                    return "Status";
            }
            return null;
        }

        @Override
        public List<ButtonItemCell<TableCellDataModel>> buttons() {
            return null;
        }
    }

    @UiConstructor
    public UserView(){
        initWidget(binder.createAndBindUi(this));

        registerUserReq = new CommitRegisterUserReq();

        driver.initialize(newUserEditor);
        driver.edit(registerUserReq);

        init();
    }

    public void init(){
        delegate = new Delegate();
        provider = new UserProvider();

        provider.setPageSize(table.getPageSize());

        table.setDelegate(delegate);
        table.setCellTableProvider(provider);

        table.initTable();
       // table.reloadData();
    }

    @UiHandler(value = "bgRefresh")
    public void onRefresh(ClickEvent clickEvent){
        table.reloadData();
    }

    @UiHandler(value = "addOK")
    public void onAddOK(ClickEvent clickEvent){
        driver.flush();
        if (driver.hasErrors()){
            Bootbox.alert("Has error!");
        }else {
            scca.addUser(registerUserReq, new MethodCallback<QueryUserRsq>() {
                @Override
                public void onFailure(Method method, Throwable throwable) {
                    String msg = throwable.getMessage();

                    addModal.hide();

                    Bootbox.alert(msg);
                }

                @Override
                public void onSuccess(Method method, QueryUserRsq queryUserRsq) {
                    newUserEditor.resetAllValues();
                    table.reloadData();
                    addModal.hide();
                }
            });
        }
    }
}
